/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"fmt"
	//"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
	
)

type SysMangerlogsController struct {
	BaseController
}

func (self *SysMangerlogsController) List() {
	self.Data["pageTitle"] = "管理员日志"

	//self.Ctx.WriteString("MangeAdminController.List")
	
	//beego.ReadFromRequest(&self.Controller);
	self.AddOperationLog("进入【管理员日志】界面",0);
	self.display();
}



func (self *SysMangerlogsController) getAdmins() {

	page,limit := 1,30;
	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	filters = append(filters, "status", 1)
	result, count := models.ManageAdminGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["login_name"] = v.LoginName
		row["real_name"] = v.RealName
		row["phone"] = v.Phone
		row["email"] = v.Email
		//row["role_ids"] = v.RoleIds
		row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
		row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
		list[k] = row
	}
	self.Data["users"] = list
	fmt.Println("成功", MSG_OK, count, list);
	//self.ajaxList("成功", MSG_OK, count, list)
}


func (self *SysMangerlogsController) Table() {
	//列表
	
	fmt.Println("====================SysMangerlogsController Table==================")
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	

	date1:=self.GetString("date1");
	date2 := self.GetString("date2");
	userName:=strings.TrimSpace(self.GetString("userName")); //userName.value,
	logContent:=strings.TrimSpace(self.GetString("logContent"))// logContent.value,


	fmt.Println(userName,logContent,date1,date2,page,limit);
	
	//time1,err := time.ParseInLocation("2006-02-01",date1,time.Local);
	//time2,err := time.ParseInLocation("2006-02-01",date2,time.Local);

	//查询条件
	filters := make([]interface{}, 0)
	
	if userName != "" {
		filters = append(filters, "Manager_name",userName)
	}
	if logContent != ""{
		filters = append(filters, "Explain__icontains", logContent)
	}


	if date1 == "" && date2 == "" {
		//什么都不干
	}else{
		if date1 == "" {
			date1 = "2006-01-02 15:04:05"
		}
	
		if date2 == "" {
			date2 = "2019-01-02 15:04:05"
		}

		//filters = append(filters, "operation_time","2017-11-07 15:00:20","2017-11-07 17:00:20")
	}

	
	fmt.Println(filters);

	//result,count := models.GMAddRecordGetListSQL("SELECT id FROM GMAddRecord  LIMIT 1,1WHERE operation_time >=\"2017-11-00 12:40:05\" and operation_time<=\"2017-11-08 12:12:10\")
	result, count := models.GMOperationLogGetList(page, self.pageSize,date1,date2, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["Id"] = v.Id;
		row["userID"] = v.Manager_id
		row["userName"] = v.Manager_name
		row["operationTime"] = v.Operation_time
		row["operationIP"] = v.Operation_IP
		row["operationResult"] = v.Status
		row["content"] = v.Explain
	
		list[k] = row
	}

	//self.AddOperationLog("查询管理员日志",0)
	self.ajaxList("成功", MSG_OK, count, list)
	
	
}
